package net.xo68.destiny.core.rpc;

import lombok.Data;

import java.util.List;

/**
 * rpc客户端
 * @author wuxie
 * @version 2020-11-5
 */
public interface RpcClient {
    final static String PROTOCOL_HTTP = "http://";
    final static String PROTOCOL_HTTPS = "https://";

    /**
     * 重试次数
     * @return
     */
    int getRetryCount();

    /**
     * get请示
     * @param host
     * @param url "/executor/log?logDateTime={1}&logId={2}&fromLineNum={3}"
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    <T> T get(String host, String url, Class<T> responseType, Object... uriVariables);

    /**
     * post请示
     * @param host
     * @param url
     * @param responseType
     * @param d
     * @param <T>
     * @param <D>
     * @return
     */
    <T,D> T postJson(String host, String url, Class<T> responseType, D d);

    /**
     * 带重试的get请求
     * @param hosts
     * @param defaultHost
     * @param url
     * @param responseType
     * @param uriVariables
     * @param <T>
     * @return
     */
    <T> T tryGet(List<String> hosts, String defaultHost, String url, Class<T> responseType, Object... uriVariables);

    /**
     * 带重试的post请求
     * @param hosts
     * @param defaultHost
     * @param url
     * @param responseType
     * @param d
     * @param <T>
     * @param <D>
     * @return
     */
    <T,D> T tryPostJson(List<String> hosts, String defaultHost, String url, Class<T> responseType, D d);

    <T,D> TryRequestResult<T> tryPostJsonAndReturnHost(List<String> hosts, String defaultHost, String url, Class<T> responseType, D d);

    String protocol();

    /**
     * 尝试请求的结果
     */
    @Data
    public static class TryRequestResult<T>{

        private String lastRequestHost;
        private T reponseResult;
    }
}
